<?php
session_start();
ob_start();
?>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="public/css/style.css" />
        <?php 
            //Require your file here
            require_once 'model/db.php';
            require_once 'template/pagination.php';
            require_once 'model/works.php';
            //Create global variable
            if(!isset($page))
                $page = 1;
            else
                $page = $_GET['page'];
            $md_works = new Works();
            $works = $md_works->select(NULL, $page, FALSE);
            $total = $md_works->select(NULL, $page, TRUE);
            
        ?>
        
    </head>
    <body>
        <div class="container">
        <!-- Include header-->
        <?php include('template/header.php')?>
            <div style="margin-left: 350px">
                <?php 
            $pagination = new pagination();
            echo $pagination->display($page, $total[0]['counter'], 'index.php');
            ?>
            </div>
            <table>
                <tr>
                    <td>Id</td>
                    <td>Name</td>
                    <?php if(isset($_SESSION['id'])): ?>
                    <td>Upload</td>
                    <?php endif; ?>
                </tr>
            <?php foreach ($works as $work):?>
                <tr>
                    <td>
                        <?php echo $work['id'] ?>
                    </td>
                    <td>
                        <?php echo $work['name'] ?>
                    </td>
                    <td>
                        <a href="view.php?id=<?php echo $work['id'] ?>">View</a>
                            <?php if(isset($_SESSION["id"])):?>
                                | <a href="edit.php?id=<?php echo $work['id'] ?>">Edit</a> |
                                <a href="delete.php?id=<?php echo $work['id'] ?>">Delete</a>
                            <?php endif;?>
                    </td>                  
                </tr>
            <?php endforeach;?>
            </table>
        
        <!-- Include footer-->
        <?php include ('template/footer.php') ?>
        </div>
    </body>
</html>
<?php    ob_end_flush()?>